<template>
  <el-button type="primary" plain @click="dialogFormVisible = true">添加商品</el-button>
  <el-table :data="orders" style="width: 100%">
    <el-table-column label="编号" prop="id" />
    <el-table-column label="商品编号" prop="productId" />
    <el-table-column label="数量" prop="count" />
    <el-table-column label="日期" prop="date" />
    <el-table-column align="right">
      <template #header>
        <el-input v-model="keyword" size="small" placeholder="搜索商品"
                  @change="loadOrder(current)"/>
      </template>
      <template #default="scope">
        <el-button size="small" @click="editOrder(scope.row)">
          更新
        </el-button>
        <el-button
            size="small"
            type="danger"
            @click="deleteOrder(scope.row.id)">
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <!--  分页-->
  <el-pagination background layout="prev, pager, next" :total="total"
                 :page-size="pageSize" @current-change="loadOrder"/>
  <!--  对话框-->
  <el-dialog v-model="dialogFormVisible" title="订单信息" width="500"
             @close="order = {}">
    <el-form :model="order">
      <el-form-item label="商品编号" >
        <el-input v-model="order.productId" autocomplete="off" />
      </el-form-item>
      <el-form-item label="数量" >
        <el-input v-model="order.count" autocomplete="off" />
      </el-form-item>
      <el-form-item label="日期" >
        <el-input v-model="order.date" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="saveOrder">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import {ElMessage,ElMessageBox} from "element-plus";
export default {
  name: "OrderView",
  data(){
    return {
      orders: [],//商品集合
      keyword: '', //搜索姓名
      current: 1, //当前页
      pageSize: 10, //页大小
      total: 10,   //总数
      dialogFormVisible: false, //对话框是否显示
      order: {},  //商品信息
    }
  },
  methods:{
    loadOrder(current){
      this.current = current
      this.$axios.get('/orders-page?current=' + this.current + "&pageSize=" +
          this.pageSize +
          "&name=" + this.keyword)
          .then(result => {
            console.log('order',result.data)
            if(result.data){
              this.orders = result.data.data;
              this.total = result.data.total;
            }
          })
    },
    //编辑商品
    editOrder(stu){
      //显示对话框
      this.dialogFormVisible = true;
      //显示商品信息  stringify object --> string  parse string ---> object
      this.order = JSON.parse(JSON.stringify(stu));
    },
    //保存商品
    saveOrder(){
      //通过id是否存在来判断是做添加还是更新
      if(this.order.id){
        //调用后台的更新商品接口
        this.$axios.put('/order',this.order)
            .then(result => {
              console.log('update',result.data)
              if(result.data){
                ElMessage.success('更新成功')
                //更新商品列表
                this.loadOrder(this.current)
              }
            })
      }else{
        //调用后台的添加商品接口
        this.$axios.post('/order',this.order)
            .then(result => {
              console.log('add',result.data)
              if(result.data){
                ElMessage.success('添加成功')
                //更新商品列表
                this.loadOrder(this.current)
              }
            })
      }
      this.dialogFormVisible = false
    },
    //删除商品
    deleteOrder(id){
      ElMessageBox.confirm(
          '是否确认删除?',
          'Warning',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          }
      ).then(() => {
        //调用后台的删除功能
        this.$axios.delete('/order/' + id)
            .then(result => {
              if(result.data){
                this.loadOrder(this.current)
              }
            })
      })
          .catch(() => {
          })
    }
  },
  mounted() {
    //默认加载第一页
    this.loadOrder(1)
  }
}
</script>
<style scoped>
</style>

